package com.jdrx.ocp.utils;



import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.Arrays;
import java.util.Base64;

import static java.security.MessageDigest.getInstance;

public class DesUtil {

    private final static String ALGORITHM = "DES";
    private final static String UNICODE_FORMAT = "utf8";

    static private Provider provider =   new BouncyCastleProvider();;

    private static SecretKeySpec secretKey;
    private static byte[] key;

    private static void setKey(String myKey) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest sha = null;

        key = myKey.getBytes("UTF-8");
        sha = getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16);
        secretKey = new SecretKeySpec(key, "AES");
    }

    public static String encrypt(String strToEncrypt, String secret) throws UnsupportedEncodingException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {

        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return Base64.getMimeEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));

    }

    public static String decrypt(String strToDecrypt, String secret) throws UnsupportedEncodingException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {

        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.getMimeDecoder().decode(strToDecrypt)));

    }

    public static String desEncrypt(String valueToEnc, String keyStr) throws Exception {
        Key key = generateKey(keyStr);
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encValue = c.doFinal(valueToEnc.getBytes(UNICODE_FORMAT));
        String encryptedValue = Base64.getMimeEncoder().encodeToString(encValue);
        return encryptedValue;
    }

    public static String desDecrypt(String encryptedValue, String keyStr) throws Exception {
        Key key = generateKey(keyStr);
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = Base64.getMimeDecoder().decode(encryptedValue);
        byte[] decValue = c.doFinal(decordedValue);//////////LINE 50
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    private static Key generateKey(String keyStr) throws Exception {

        MessageDigest digester = MessageDigest.getInstance("SHA-256", provider);
        digester.update(keyStr.getBytes(UNICODE_FORMAT));
        byte[] key = digester.digest();
        SecretKeySpec spec = new SecretKeySpec(key, ALGORITHM);

        return spec;
    }

}